package io.litchi.auth.application.service;

import io.litchi.auth.Infrastructure.common.command.menupern.MenuPernSaveCommand;
import io.litchi.auth.Infrastructure.common.command.menupern.MenuPernUpdateStatusCommand;
import io.litchi.auth.Infrastructure.common.result.menu.TreeMenuResult;
import io.litchi.auth.domain.entity.auth.MenuPerm;
import io.litchi.auth.domain.factory.MenuPermFactory;
import io.litchi.auth.domain.repository.MenuPermRepository;
import io.litchi.common.exception.LitchiException;
import io.litchi.common.result.Result;
import io.litchi.common.tree.util.TreeUtil;
import jakarta.validation.Valid;
import jakarta.validation.Validator;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.ArrayList;

import java.util.List;

/**
 *
 *
 * @author helang
 * @since 2025-11-25
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class MenuService {

    private static final Integer MENU_TYPE = 0;
    private static final Integer PERN_TYPE = 1;
    private final MenuPermRepository menuPermRepository;
    private final MenuPermFactory menuPermFactory;
    private final Validator validator;

    /**
     * 查询树形菜单列表
     *
     * @return treeList
     */
    public Result<List<TreeMenuResult>> treeList() {
        List<MenuPerm> menuPermList = menuPermRepository.findAll();

        if (menuPermList.isEmpty()) {
            return Result.success(new ArrayList<>());
        }
        List<TreeMenuResult> treeMenuResultList = menuPermFactory.mapToTreeMenuList(menuPermList);
        List<TreeMenuResult> resultList = (List<TreeMenuResult>) TreeUtil.toTree(treeMenuResultList, null);
        return Result.success(resultList);
    }

    /**
     * 保存或者权限
     *
     * @param menuPernSaveCommand menuPernSaveCommand
     * @return void
     */
    public Result<Void> save(MenuPernSaveCommand menuPernSaveCommand) {
        log.info("保存菜单参数:{}", menuPernSaveCommand);
        //校验参数
        menuPernSaveCommand.validate(validator);
        //save
        MenuPerm menuPerm = menuPermFactory.mapToEntity(menuPernSaveCommand);
        menuPermRepository.save(menuPerm);
        return Result.success();
    }


    /**
     * 详情
     *
     * @param id id
     * @return MenuPernSaveCommand
     */
    public Result<MenuPernSaveCommand> detail(Long id) {
        log.info("查询详情id:{}", id);
        MenuPerm menuPerm = menuPermRepository.findById(id).orElseThrow(() -> new LitchiException("未知id"));
        MenuPernSaveCommand menuPernSaveCommand = menuPermFactory.mapToCommand(menuPerm);
        return Result.success(menuPernSaveCommand);
    }

    /**
     * 删除
     *
     * @param id id
     * @return void
     */
    @Transactional
    public Result<Void> delete(Long id) {
        log.info("删除id:{}", id);
        Long count = menuPermRepository.countByPid(id);
        if (count > 0) {
            throw new LitchiException("当前菜单还有下级菜单或权限删除");
        }
        menuPermRepository.deleteById(id);
        return Result.success();
    }

    /**
     * 修改菜单状态
     *
     * @param menuPernUpdateStatusCommand 参数
     * @return void
     */
    @Transactional
    public Result<Void> updateStatus(@Valid MenuPernUpdateStatusCommand menuPernUpdateStatusCommand) {
        log.info("更新菜单状态参数:{}", menuPernUpdateStatusCommand);
        menuPermRepository.updateStatusById(menuPernUpdateStatusCommand.getId(), menuPernUpdateStatusCommand.getStatus());
        return Result.success();
    }
}